package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.web.entity.BoSpaceUser;
import com.chy.blog_server.modular.web.service.IBoSpaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：空间用户表 controller
 * 作者：chenhuiyan
 * 时间：2020/04/02
 */
@Controller
@RequestMapping(value = "/web/boSpaceUser")
public class BoSpaceUserController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boSpaceUser/";

    @Autowired
    private IBoSpaceUserService boSpaceUserService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boSpaceUser";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boSpaceUserService.list(params); //获取数据列表
            Integer count = boSpaceUserService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(){
        return PREFIX + "boSpaceUser_add";
    }

    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boSpaceUser_edit";
    }

    /**
     * 保存
     * @param boSpaceUser
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoSpaceUser boSpaceUser, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            if(StrUtil.isEmpty(boSpaceUser.getId())){

                //判断帐号是否存在
                BoSpaceUser su = boSpaceUserService.getOne(new QueryWrapper<BoSpaceUser>().eq("account", boSpaceUser.getAccount()).eq("del_flag", 0));
                if(su != null){
                    res.put("state", "500");
                    res.put("message", "该帐号已存在！");
                    return res;
                }

                boSpaceUser.preInsert(request);
                boSpaceUser.setPassword(SecureUtil.md5(boSpaceUser.getPassword())); //加密
                //默认头像
                if(StrUtil.isEmpty(boSpaceUser.getHeadUrl())){
                    boSpaceUser.setHeadUrl("moren.jpg");
                }
                boSpaceUserService.save(boSpaceUser);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                boSpaceUser.preUpdate(request);
                boSpaceUserService.updateById(boSpaceUser);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boSpaceUserService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 上传头像
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadHeadPhoto")
    @ResponseBody
    public Map<String, Object> uploadHeadPhoto(MultipartFile file){
        //上传
        Map<String, Object> res = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.HEAD_PHOTO, false);

        return res;
    }

    /**
     * 跳转修改密码页面
     * @return
     */
    @RequestMapping(value = "/onPassword")
    public String onPassword(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "update_password";
    }

    /**
     * 修改密码
     * @param boSpaceUser
     * @param request
     * @return
     */
    @RequestMapping(value = "/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(BoSpaceUser boSpaceUser, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            BoSpaceUser spaceUser = boSpaceUserService.getById(boSpaceUser.getId());
            spaceUser.setPassword(SecureUtil.md5(boSpaceUser.getPassword()));
            spaceUser.preUpdate(request);
            boSpaceUserService.updateById(spaceUser);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 跳转修改状态页面
     * @return
     */
    @RequestMapping(value = "/onStatus")
    public String onStatus(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "update_status";
    }

    /**
     * 修改帐号状态
     * @param boSpaceUser
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(BoSpaceUser boSpaceUser, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            BoSpaceUser spaceUser = boSpaceUserService.getById(boSpaceUser.getId());
            spaceUser.setStatus(boSpaceUser.getStatus());
            spaceUser.preUpdate(request);
            boSpaceUserService.updateById(spaceUser);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoSpaceUser boSpaceUser = boSpaceUserService.getById(id);
            boSpaceUser.setDelFlag(1);
            boSpaceUser.preUpdate(request);
            boSpaceUserService.updateById(boSpaceUser);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

}
